package cn.wzy.service.impl;


import cn.wzy.mapper.EmployeeMapper;
import cn.wzy.pojo.Employee;

import cn.wzy.results.ResponseResult;
import cn.wzy.service.EmployeeService;
import cn.wzy.utils.RedisCache;
//import cn.wzy.utils.UploadingFile;
import cn.wzy.vo.EmpRoleVo;
import cn.wzy.vo.GoPageVo;
import cn.wzy.vo.PageVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * (Employee)表服务实现类
 *
 * @author makejava
 * @since 2022-12-08 13:53:14
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper,Employee>implements EmployeeService {
    @Resource
    private EmployeeMapper employeeMapper;

    @Resource
    private RedisCache redisCache;
    /*
    * 分页查询
    * */
    @Override
    public ResponseResult employeePage(GoPageVo goPageVo) {
//        偏移量
        Long starter = (goPageVo.getPageIndex()-1)* goPageVo.getPageSize();
//      得到分页员工
        List<Employee> employees = employeeMapper.employeePage(starter,goPageVo.getPageSize(),goPageVo.getKeyword());
//        总数
        Long total = employeeMapper.getTotal(goPageVo);
//        封装
        PageVo pageVo = new PageVo(employees, total, goPageVo.getPageSize());
        return ResponseResult.okResult(pageVo);
    }

    /*
    * 添加/修改员工
    * */
    @Override
    @Transactional
    public ResponseResult saveEmp(Employee employee) {
//        使用上传工具类
//        String fileName = UploadingFile.upload(fileUpload);
//            判断是否为空
//        if (fileName == null){
//            return ResponseResult.errorResult(AppHttpCodeEnum.FILE_TYPE_ERROR);
//        }
//           封装
//        employee.setHeadImage(fileName);
//        判断id
        if (Objects.isNull(employee.getId())){
//            添加
//           添加到数据库
            employeeMapper.addEmp(employee);
            return ResponseResult.okResult();
        }else {
//            修改
            employeeMapper.updateEmp(employee);
            return ResponseResult.okResult();
        }
    }

    /*
    * 删除员工
    * */
    @Override
    @Transactional
    public ResponseResult delEmp(Long id) {
        employeeMapper.delEmp(id);
        return ResponseResult.okResult();
    }

    /*
     * 下载头像
     * */
    @Override
    public ResponseResult download(String fileName,HttpServletResponse response) {
//        UploadingFile.download(fileName,response);
        return ResponseResult.okResult();
    }

    /*
    * 批量删除
    * */
    @Override
    @Transactional
    public ResponseResult delEmpList(List<Long> ids) {
        employeeMapper.delEmpList(ids);
        return ResponseResult.okResult();
    }

    /*
    * 获取所有员工
    * */
    @Override
    public ResponseResult getAllEmp() {
        List<Employee> employees = employeeMapper.getAllEmp();
        return ResponseResult.okResult(employees);
    }

    /*
    * 设置员工角色
    * */
    @Override
    @Transactional
    public ResponseResult saveEmpRole(EmpRoleVo empRoleVo) {
//        通过id删除员工角色
        employeeMapper.delEmpRole(empRoleVo.getId());
//        批量查询到对应Sn的角色id
        List<Long> roleIds = employeeMapper.getEmpRoleIdsBySns(empRoleVo.getRoleSns());
//        批量添加员工角色
        employeeMapper.addEmpRole(empRoleVo.getId(),roleIds);
        return ResponseResult.okResult();
    }

    /*
    * 获取员工角色
    * */
    @Override
    public ResponseResult getEmpRoleSns(Long id) {
        List<String> roleSns = employeeMapper.getEmpRoleSnsById(id);
        return ResponseResult.okResult(roleSns);
    }

    /*
    * 通过token获取员工信息
    * */
    @Override
    public ResponseResult getEmpByToken(HttpServletRequest request) {
        String token = request.getHeader("token");
        Employee emp = redisCache.getCacheObject("User:" + token);
        return ResponseResult.okResult(emp);
    }
}
